(function($) {
	//全局配置(通常所有页面引用该配置，特殊页面使用mui.init({})来覆盖全局配置)
	$.initGlobal({
		swipeBack: true
	});
	var back = $.back;
	$.back = function() {
		var current = plus.webview.currentWebview();
		if (current.mType === 'main') { //模板主页面
			current.hide('auto');
			setTimeout(function() {
				document.getElementById("title").className = 'mui-title mui-fadeout';
				current.children()[0].hide("none");
			}, 200);
		} else if (current.mType === 'sub') {
			if ($.targets._popover) {
				$($.targets._popover).popover('hide');
			} else {
				current.parent().evalJS('mui&&mui.back();');
			}
		} else {
			back();
		}
	}
})(mui);

/**
 * toggle
 */
window.addEventListener('toggle', function(event) {
	if (event.target.id === 'M_Toggle') {
		var isActive = event.detail.isActive;
		var table = document.querySelector('.mui-table-view');
		var card = document.querySelector('.mui-card');
		if (isActive) {
			card.appendChild(table);
			card.style.display = '';
		} else {
			var content = document.querySelector('.mui-content');
			content.insertBefore(table, card);
			card.style.display = 'none';
		}
	}
});


(function(window){
	var o = {};
	
	mui('body').on('tap', 'a', function(e) {
		
		//判断是否需要登录
		if(this.getAttribute('need-login') == "true") {
			if(!_API.getPeople()){
				plus.nativeUI.toast("请先登录");
				newWindow("/view/2people/login.html");
				return ;
			}
		}
		var href = this.getAttribute('href')+"";
		if(href && (href.toLowerCase().indexOf("http")==0)) {
			e.stopPropagation();
			plus.runtime.openURL(href);
			return false;
		}

		//开始
		var id = this.getAttribute('new-window');
		if((!id) || "#"==id){
			return;
		}
		
		var time = new Date().getTime();
		if(!o[id]){
			o[id] = 0;
		}
		if(time - o[id] > 1000) {//防止点击过快
			o[id] = time;
			var i = this.getAttribute('new-window-data-index');
			newWindow(id, __data[i]);
		}		
	});
	
	var __data = [];
	window.pushNewWindowData = function(data){
		if(data) {
			__data = data;
		}
	}
	
})(window);

var newWindow = function(url, data){
	mui.openWindow({
		id: url,
		url: url,
		styles: {
			top: 0,
			bottom: 0
		},
		waiting: {
			autoShow: true
		},
	    extras:data
	});
}
